# -*- coding: UTF-8 -*-  

import datetime
import sqlalchemy
from sqlalchemy import create_engine, Column
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.mysql import VARCHAR, BIGINT, LONGTEXT, INTEGER, DATETIME
from dante import app

engine = create_engine(app.config['DATABASE_URI'], convert_unicode=True, pool_recycle=3600)
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
Model = declarative_base(name='Model')
Model.query = db_session.query_property()

class Sms(Model):
	REQUEST = 0
	SUCCESS = 1
	ERROR = 2
	__tablename__ = 'sms'

	id = Column(BIGINT(20), primary_key=True)
	request_time = Column(DATETIME)
	mobile = Column(VARCHAR(255))
	content = Column(VARCHAR(255))
	status = Column(INTEGER(10))
	schedule_time = Column(DATETIME)
	send_time = Column(DATETIME)
	source_ip = Column(VARCHAR(255))
	yunpian_result = Column(VARCHAR(255))

	def __init__(self, mobile, content, schedule_time, source_ip):
		self.mobile = mobile
		self.content = content
		self.status = Sms.REQUEST
		self.request_time = datetime.datetime.now()
		self.schedule_time = schedule_time
		self.source_ip = source_ip

	def to_dict(self):
		return dict(id=self.id, mobile=self.mobile, content=self.content, schedule_time=self.schedule_time, status=self.status, source_ip=self.source_ip, request_header=self.request_header)

Model.metadata.create_all(engine)
